<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-header">
        <h2><i class="fas fa-user"></i> 用户登录</h2>
        <p>请输入您的账号信息</p>
      </div>
      
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username"><i class="fas fa-user"></i> 用户名</label>
          <input 
            type="text" 
            id="username" 
            v-model="username" 
            required
            placeholder="输入用户名"
          >
        </div>
        
        <div class="form-group">
          <label for="password"><i class="fas fa-lock"></i> 密码</label>
          <div class="password-input-wrapper">
            <input 
              :type="showPassword ? 'text' : 'password'" 
              id="password" 
              v-model="password" 
              required
              placeholder="输入密码"
            >
            <span class="toggle-password" @click="togglePasswordVisibility">
              <i :class="showPassword ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
            </span>
          </div>
        </div>
        
        <button type="submit" class="login-btn" :disabled="isLoading">
          <span v-if="isLoading"><i class="fas fa-spinner fa-spin"></i> 登录中...</span>
          <span v-else>登录</span>
        </button>
        
        <p class="register-link">
          还没有账号？<router-link to="/register">立即注册</router-link>
        </p>
        
        <div v-if="error" class="error-message">
          <i class="fas fa-exclamation-circle"></i> {{ error }}
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/store/user'
import { userApi } from '@/api/api'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const username = ref('')
const password = ref('')
const showPassword = ref(false)
const error = ref('')
const isLoading = ref(false)

const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value
}

const handleLogin = async () => {
  try {
    isLoading.value = true
    error.value = ''
    
    const response = await userApi.login({
      username: username.value,
      password: password.value,
      email: null
    })

    localStorage.setItem('token', response.token)
    userStore.setUser({
      username: response.username,
      isLoggedIn: true,
      token: response.token,
      userId: response.user_id,
    })

    // 登录后重定向逻辑
    const redirectPath = route.query.redirect || (userStore.isAdmin ? '/admin/dashboard' : '/')
    await router.push(redirectPath)
    
  } catch (err) {
    error.value = err.message || '登录失败，请稍后重试'
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  background-color: #f5f7fa;
}

.login-container {
  background: white;
  padding: 2.5rem;
  border-radius: 10px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 450px;
}

.login-header {
  text-align: center;
  margin-bottom: 2rem;
}

.login-header h2 {
  color: #2c3e50;
  margin-bottom: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.login-header p {
  color: #7f8c8d;
  font-size: 0.95rem;
}

.login-form .form-group {
  margin-bottom: 1.5rem;
}

.login-form label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 600;
  color: #2c3e50;
  display: flex;
  align-items: center;
  gap: 8px;
}

.password-input-wrapper {
  position: relative;
}

.login-form input {
  width: 100%;
  padding: 0.85rem;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 1rem;
  transition: border 0.3s;
}

.login-form input:focus {
  outline: none;
  border-color: #3498db;
}

.toggle-password {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: #7f8c8d;
}

.login-btn {
  width: 100%;
  padding: 0.85rem;
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.3s;
  margin-top: 1rem;
}

.login-btn:hover {
  background-color: #2980b9;
}

.login-btn:disabled {
  background-color: #bdc3c7;
  cursor: not-allowed;
}

.register-link {
  text-align: center;
  margin-top: 1.5rem;
  color: #7f8c8d;
  font-size: 0.9rem;
}

.register-link a {
  color: #3498db;
  text-decoration: none;
}

.register-link a:hover {
  text-decoration: underline;
}

.error-message {
  background-color: #fdecea;
  color: #d32f2f;
  padding: 0.75rem;
  border-radius: 6px;
  margin-top: 1.5rem;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
}
</style>